FROM ubuntu:22.10
COPY cross.meson /build/buildFFmpeg/
COPY windows-cross-toolchain.cmake /build/buildFFmpeg/
COPY build.sh /build/buildFFmpeg/
COPY buildDependents.sh /build/buildFFmpeg/
COPY ffmpeg-deps /build/ffmpeg-deps
RUN apt update \
    && apt install -y wget git make automake autoconf gcc g++ gdb python3 python3-pip meson cmake pkg-config libtool gperf groff mercurial flex bison texinfo autopoint gettext subversion ragel \
    && pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip3 install meson ninja jinja2 \
    && wget "https://gitee.com/rocklakegrass/ffmpeg-windows-arm64/releases/download/llvm-mingw-mirror/llvm-mingw-20220906-ucrt-ubuntu-18.04-x86_64.tar.xz" \
    && xz -d "llvm-mingw-20220906-ucrt-ubuntu-18.04-x86_64.tar.xz" \
    && tar -xvf "llvm-mingw-20220906-ucrt-ubuntu-18.04-x86_64.tar" \
    && mv "llvm-mingw-20220906-ucrt-ubuntu-18.04-x86_64" /opt/llvm-mingw \
    && rm -r "llvm-mingw-20220906-ucrt-ubuntu-18.04-x86_64.tar" \
    && sed -i "200,209d" /opt/llvm-mingw/aarch64-w64-mingw32/include/string.h \
    && sed -i "199a void *memcpy(void *dest, const void *src, size_t n) {  \n\
    if (dest == NULL || src == NULL) return NULL;   \n\
    int *pdst = (int *) dest;   \n\
    int *psrc = (int *) src;    \n\
    char *byte1, *byte2;    \n\
    int cnt1 = n / 4, cnt2 = n % 4; \n\
    if ((char *) pdst > ((char *) psrc + n) || pdst < psrc) {   \n\
        while (cnt1--) *pdst++ = *psrc++;   \n\
        byte1 = (char *) pdst;  \n\
        byte2 = (char *) psrc;  \n\
        while (cnt2--) *byte1++ = *byte2++;     \n\
    } else {    \n\
        byte1 = (char *) pdst + n - 1;  \n\
        byte2 = (char *) psrc + n - 1;  \n\
        while (cnt2--) *byte1-- = *byte2--; \n\
        byte1++, byte2++;   \n\
        pdst = (int *) byte1 - 1;   \n\
        psrc = (int *) byte2 - 1;   \n\
        while (cnt1--) *pdst-- = *psrc--;   \n\
    }   \
    return dest;    \n\
}   \n\
\n\
__mingw_bos_extern_ovr  \n\
void *memset(void *s, int c, size_t n) {    \n\
    char *tmp = (char *) s; \n\
    while (n)   \n\
        *tmp++ = (char) c, n--; \n\
    return s;   \n\
}   \n\
" /opt/llvm-mingw/aarch64-w64-mingw32/include/string.h \
    && chmod -R 777 /build/buildFFmpeg/build.sh
ENV PATH=$PATH:/opt/llvm-mingw/bin \
    FFBUILD_PREFIX="/build/ffmpeg-deps" \
    FFBUILD_CROSS_PREFIX="aarch64-w64-mingw32-" \
    FFBUILD_TOOLCHAIN="aarch64-w64-mingw32" \
    PKG_CONFIG_PATH="/build/ffmpeg-deps/lib/pkgconfig:/build/ffmpeg-deps/lib64/pkgconfig" \
    FFBUILD_CMAKE_TOOLCHAIN=/build/buildFFmpeg/windows-cross-toolchain.cmake \
    CWD=/build/buildFFmpeg